<?php 
/**
 * @package framework
 * @subpackage controllers
 *
 * @author Philippe Archambault <philippe.archambault@gmail.com>
 * @version 0.1
 * @license http://www.gnu.org/licenses/gpl.html GPL License
 * @copyright Philippe Archambault, 2008
 */

/**
 * Class LocationController
 *
 * @package framework
 * @subpackage controllers
 *
 * @since 0.1
 */
class LocationController extends Controller
{

    public function __construct()
    {
        AuthUser::load();
        if ( ! AuthUser::isLoggedIn())
            redirect(get_url('login'));
        $this->setLayout('backend');
        $this->assignToLayout('sidebar', new View('location/sidebar'));
    }
    
    public function index()
    {
		
        if ( ! AuthUser::hasPermission('administrator'))
        {
            Flash::set('error', __('You do not have permission to access the requested page!'));

            if (Setting::get('default_tab') === 'location')
                redirect(get_url('page'));
            else
                redirect(get_url());
        }
        
        $this->display('location/index', array(
            'locations' => Location::findAll()
        ));
    }
    
    public function add()
    {
        if ( ! AuthUser::hasPermission('administrator'))
        {
            Flash::set('error', __('You do not have permission to access the requested page!'));
            redirect(get_url());
        }
        
        // check if trying to save
        if (get_request_method() == 'POST')
            return $this->_add();
        
        // check if location have already enter something
        $location = Flash::get('post_data');
        
        if (empty($location))
            $location = new Location;
        
        $this->display('location/edit', array(
            'action' => 'add',
            'locatioin' => $location,
            'permissions' => Record::findAllFrom('Permission')
        ));
    }
    
    private function _add()
    {
        $data = $_POST['location'];
        
        Flash::set('post_data', (object) $data);
        
        // check if name > 0 chars
        if (empty($data['name']))
        {
            Flash::set('error', __('You have to specify a name!'));
            redirect(get_url('location/add'));
        }
        
        $location = new Location($data);
		
        if ($location->save())
        {
            Flash::set('success', __('Location has been added!'));
            Observer::notify('location_after_add', $location->name);
        }
        else Flash::set('error', __('Location has not been added!'));
        
        redirect(get_url('location'));
    }
    
    public function edit($id)
    {
        if ( AuthUser::getId() != $id && ! AuthUser::hasPermission('administrator'))
        {
            Flash::set('error', __('You do not have permission to access the requested page!'));
            redirect(get_url());
        }
        
        // check if trying to save
        if (get_request_method() == 'POST')
            return $this->_edit($id);
        
        if ($location = Location::findById($id))
        {
            $this->display('location/edit', array(
                'action' => 'edit', 
                'location' => $location,
                'permissions' => Record::findAllFrom('Permission')
            ));
        }
        else Flash::set('error', __('Location not found!'));
        
        redirect(get_url('location'));
        
    } // edit
    
    private function _edit($id)
    {
        $data = $_POST['location'];
        
        // check if name > 0 chars
        if (empty($data['name']))
        {
            Flash::set('error', __('You have to specify a name!'));
            redirect(get_url('location/edit/'.$id));
        }
        
        $location = Record::findByIdFrom('Location', $id);
        $location->setFromData($data);
        
        if ($location->save())
        {
            Flash::set('success', __('Location has been saved!'));
            Observer::notify('location_after_edit', $location->name);
        }
        else Flash::set('error', __('Location has not been saved!'));
        
            redirect(get_url('location'));
    }
    
    public function delete($id)
    {
        if ( ! AuthUser::hasPermission('administrator'))
        {
            Flash::set('error', __('You do not have permission to access the requested page!'));
            redirect(get_url());
        }
        
        // find the location to delete
		if ($location = Record::findByIdFrom('Location', $id))
		{
			if ($location->delete())
			{
				Flash::set('success', __('Location <strong>:name</strong> has been deleted!', array(':name' => $location->name)));
				Observer::notify('location_after_delete', $location->name);
			}
			else
				Flash::set('error', __('Location <strong>:name</strong> has not been deleted!', array(':name' => $location->name)));
		}
		else Flash::set('error', __('Location not found!'));
        
        redirect(get_url('location'));
    }

} // end LocationController class
